import react from "@vitejs/plugin-react";
import vitePluginImp from "vite-plugin-imp";
import { fileURLToPath, URL } from "url";
import { VitePWA } from "vite-plugin-pwa";
import dayjs from "dayjs";
import zipPack from "vite-plugin-zip-pack";

// https://vitejs.dev/config/
export default ({ mode }) => {
  const isDev = mode === "development";
  const isProd = mode === "production";

  const nowDate = dayjs().format("YYYY-MM-DD_hh:mm");

  return {
    base: "./",
    // 全局替换
    define: {
      "process.env.BUILD_TIME": `'${nowDate}'`,
    },

    build: {
      assetsInlineLimit: 0,
      minify: "terser",
      terserOptions: {
        compress: {
          drop_console: false,
          drop_debugger: true,
        },
      },
      chunkSizeWarningLimit: 1024 * 100,
      rollupOptions: {
        input: {
          index: fileURLToPath(new URL("./index.html", import.meta.url)),
          wx_qrcode: fileURLToPath(
            new URL("./wx_qrcode.html", import.meta.url)
          ),
          scan_code_success: fileURLToPath(
            new URL("./scan_code_success.html", import.meta.url)
          ),
        },
      },
    },
    plugins: [
      react(),
      vitePluginImp({
        optimize: true,
        libList: [
          {
            libName: "antd",
            libDirectory: "es",
            style: (name) => `antd/es/${name}/style`,
          },
          {
            libName: "antd-mobile",
            style: () => false,
            libDirectory: "es/components",
            replaceOldImport: true,
          },
        ],
      }),
      isProd &&
        VitePWA({
          scope: "/vite_react_mobx_threebase/",
          mode,
          minify: false,
          registerType: "autoUpdate",
          workbox: {
            // 自定义前端缓存的文件
            globPatterns: [
              "**/*.{js,css,html,png,jpg,jpeg,gif,ico,svg,wasm,glb,gltf,fbx,pdf,mp3,mp4,ttf,zip}",
            ],
            maximumFileSizeToCacheInBytes: 1024 * 1024 * 50, // 最大50MB
          },
        }),
    ],
    resolve: {
      alias: {
        "@hashHistory": fileURLToPath(
          new URL("./src/hashHistory", import.meta.url)
        ),
        "@utils": fileURLToPath(
          new URL("./src/utils/utils.ts", import.meta.url)
        ),
        "@views": fileURLToPath(new URL("./src/views", import.meta.url)),
        "@globalStore": fileURLToPath(
          new URL("./src/globalStore.ts", import.meta.url)
        ),
        "@components": fileURLToPath(
          new URL("./src/components", import.meta.url)
        ),
        "@api": fileURLToPath(new URL("./src/api", import.meta.url)),
        "@NewMap": fileURLToPath(
          new URL("./src/utils/NewMap.ts", import.meta.url)
        ),
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true, // 支持内联 javascript
        },
      },
    },
    server: {
      port: 5333,
      proxy: {
        "/api": {
          target: "https://api.open3dcloud.com/test/klbg",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
  };
};
